package com.library.cx.service;


import com.library.cx.dao.Libraryimpl;
import com.library.cx.dao.Librarys;
import com.library.cx.into.Libary;
import com.library.cx.util.MybatisUtil;
import com.library.cx.util.Page;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class Totalimpl implements Total{
    @Override
    public Page findByPage(String name, String author, Integer currentPage) {
        //新建分页对象
       Page page=new Page();
        //设置每页的显示行数
        page.setPageSize(10);
        //设置当前页码
        page.setCurrentPage(currentPage);
        //获取总记录数调用dao层去查询
        Libraryimpl libraryimpl=new Libraryimpl();
        SqlSession sqlSession = MybatisUtil.getSqlSession();
        int count = sqlSession.getMapper(Librarys.class).totalCount();
        //设置总记录数
        page.setTotalCount(count);
        //根据当前页查出起始下标
        int sum=(currentPage-1)*page.getPageSize();
        //调用dao查询当前页的数据的集合
        Map map=new HashMap<>();
        map.put("name",name);
        map.put("author",author);
        map.put("sum",sum);
        map.put("page.getPageSize()",page.getPageSize());
        List<Libary> finall = sqlSession.getMapper(Librarys.class).finall(map);
        //存入分页对象
        page.setList(finall);
        //将分页对象返回
        return page;
    }

    /**
     * 随机书位置
     * @return
     */
    public static String getRandomString(){
        String str="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
        String str2="0123456789";
        Random random=new Random();
        StringBuffer sb=new StringBuffer();
        int number=random.nextInt(26);
        int number2=random.nextInt(10);
        sb.append(str.charAt(number));
        sb.append("-");
        sb.append(str2.charAt(number2));

        return sb.toString();
    }
}
